﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IsPay
{
    public partial class paybatch : System.Web.UI.Page
    {
        public string tbBody;
        public string orderCount;
        public string sumAmonut;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string action = Request.Params["action"];
                if (string.IsNullOrEmpty(action))
                {

                }
                else
                {
                    if (action.Equals("entry") && Request.Params["postData"] != null)
                    {
                        string postData = Request.Params["postData"].Trim();

                        MakeTableBody(postData);

                        lblOrderCount.Text = "订单总笔数:" + orderCount;
                        lblSumAmonut.Text = "总金额:" + sumAmonut;
                    }
                    else if (action.Equals("fileupload"))
                    {
                        payfileupload page = (payfileupload)Context.Handler;
                        DataTable dtFile = page.dtFile;

                        MakeTableBody(dtFile);

                        lblOrderCount.Text = "订单总笔数:" + orderCount;
                        lblSumAmonut.Text = "总金额:" + sumAmonut;
                    }
                }
            }
        }

        private void MakeTableBody(string postData)
        {
            string body = "";

            string[] arrPost = postData.Split('|');
            if (arrPost.Length != 4)
            {
                return;
            }

            string[] arrCardType = arrPost[0].Split(',');
            string[] arrCardNo = arrPost[1].Split(',');
            string[] arrCardName = arrPost[2].Split(',');
            string[] arrAmounts = arrPost[3].Split(',');

            int isum = 0;

            for (int idx = 0; idx < arrCardType.Length; idx++)
            {
                isum += int.Parse(arrAmounts[idx]);

                body += "<tr><td class='cardType'>" + arrCardType[idx] + "</td><td class='cardName'>" + arrCardName[idx] 
                    + "</td><td class='cardNo'>" + arrCardNo[idx] + "</td><td class='amount'>" + arrAmounts[idx] + "</td></tr>";
            }

            tbBody = body;
            orderCount = arrCardType.Length.ToString();
            sumAmonut = isum.ToString();

        }

        private void MakeTableBody(DataTable dtFile)
        {
            if (dtFile == null || dtFile.Rows.Count <= 0)
            {
                return;
            }

            int iBankName = -1;
            int iCareNo = -1;
            int iName = -1;
            int iAmonut = -1;
            for (int i = 0; i < dtFile.Columns.Count; i++)
            {
                //银行,卡号,姓名,金额
                if (dtFile.Columns[i].ColumnName.IndexOf("银行") >= 0 && dtFile.Columns[i].ColumnName.IndexOf("卡号") < 0)
                {
                    iBankName = i;
                }
                else if (dtFile.Columns[i].ColumnName.IndexOf("卡号") >= 0)
                {
                    iCareNo = i;
                }
                else if (dtFile.Columns[i].ColumnName.IndexOf("姓名") >= 0)
                {
                    iName = i;
                }
                else if (dtFile.Columns[i].ColumnName.IndexOf("金额") >= 0)
                {
                    iAmonut = i;
                }
            }

            if (iBankName < 0 || iCareNo < 0 || iName < 0 || iAmonut < 0)
            {
                return;
            }

            float isum = 0;
            string body = "";
            foreach (DataRow dr in dtFile.Rows)
            {
                string s = dr[iAmonut].ToString();
                isum += float.Parse(dr[iAmonut].ToString().Trim());

                body += "<tr><td>" + dr[iBankName].ToString()
                    + "</td><td>" + dr[iName].ToString()
                    + "</td><td>" + dr[iCareNo].ToString()
                    + "</td><td>" + dr[iAmonut].ToString()
                    + "</td></tr>";
            }

            tbBody = body;
            orderCount = dtFile.Rows.Count.ToString();
            sumAmonut = isum.ToString();
        }

    }
}